#!/bin/bash

KERNEL_DIR := /lib/modules/$(shell uname -r)/build

CC := gcc
CFLAGS= -g -Wall -Wno-unused -Wformat

TOP_DIR := $(shell pwd)

all:
	@echo "**************************** Start Make Project *********************************"
	mkdir -p $(TOP_DIR)/output
	$(MAKE) -C $(TOP_DIR)/kernel-driver TOP_DIR=$(shell pwd) CC=$(CC) KDIR=$(KERNEL_DIR) ko_module
	cp $(TOP_DIR)/kernel-driver/*.ko $(TOP_DIR)/output -rf
	$(MAKE) -C $(TOP_DIR)/lib TOP_DIR=$(shell pwd) CC=$(CC) qtsm_lib
	cp $(TOP_DIR)/lib/libqtsm.so $(TOP_DIR)/output/ -rf
	$(MAKE) -C $(TOP_DIR)/tests TOP_DIR=$(shell pwd) CC=$(CC) gtest_libqtsm
	cp $(TOP_DIR)/tests/gtest_libqtsm $(TOP_DIR)/output/ -rf
	@echo "**************************** Make Project Finished ******************************"

qtsm_lib:
	@echo "**************************** Start Make Project *********************************"
	mkdir -p $(TOP_DIR)/output
	$(MAKE) -C $(TOP_DIR)/lib TOP_DIR=$(shell pwd) CC=$(CC) qtsm_lib
	cp $(TOP_DIR)/lib/libqtsm.so $(TOP_DIR)/output/ -rf
	@echo "**************************** Make Project Finished ******************************"

clean:
	$(MAKE) -C $(TOP_DIR)/kernel-driver TOP_DIR=$(shell pwd) CC=$(CC) KDIR=$(KERNEL_DIR) clean
	$(MAKE) -C $(TOP_DIR)/lib TOP_DIR=$(shell pwd) CC=$(CC) clean
	$(MAKE) -C $(TOP_DIR)/tests TOP_DIR=$(shell pwd) CC=$(CC) clean
	rm $(TOP_DIR)/output/* -rf
